package com.manlitech.manlichat.base.netty.handler;/*
 *Created by Intellij IDEA.
 *User:  Shensg
 *Date:  2021/1/19
 */

import com.google.protobuf.InvalidProtocolBufferException;
import com.manlitech.manlichat.base.netty.util.MessageSendUtil;
import com.manlitech.manlichat.proto.GameMsgProtocol;
import com.manlitech.manlichat.proto.ProtoMsg;
import com.manlitech.manlichat.util.ResultUtil;
import com.manlitech.manlichat.util.websocketclient.ByteUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * websocket 二进制处理 binary
 * @author Shensg
 */
@Component
@ChannelHandler.Sharable
public class MyBinaryWebSocketFrameHandler extends SimpleChannelInboundHandler<BinaryWebSocketFrame> {
    private static final Logger log = LoggerFactory.getLogger(MyBinaryWebSocketFrameHandler.class);

    @Autowired
    MessageSendUtil messageSendUtil;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, BinaryWebSocketFrame msg) throws InterruptedException, InvalidProtocolBufferException {


        log.info("收到客户端消息, msgClzz={},msgBody = {} ,消息内容长度:[{}]",
                msg.getClass().getName(),msg ,msg.content().capacity());

        // 内容（标识和内容）
        ByteBuf byteBuf = msg.content();
        // 标识符
        int signIndexLength = 2 ;
        byte[] signBytes ;
        String sign ;
        // 内容
        byte[] msgBytes;
        String msgStr;

        if(byteBuf.hasArray()) {
            // 处理堆缓冲区
            log.info("---处理堆缓冲区---");
            signBytes = ByteUtils.subBytes(byteBuf.array(),0,signIndexLength);
            sign = new String(signBytes);
            msgBytes = ByteUtils.subBytes(byteBuf.array(),signIndexLength,byteBuf.readableBytes()-signIndexLength);

        } else {
            log.info("---处理直接缓冲区以及复合缓冲区---");
            // 处理直接缓冲区以及复合缓冲区
            byte[] bytes = new byte[byteBuf.readableBytes()];
            byteBuf.getBytes(byteBuf.readerIndex(), bytes);

            signBytes = ByteUtils.subBytes(bytes,0,signIndexLength);
            sign = new String(signBytes);
            msgBytes = ByteUtils.subBytes(bytes,signIndexLength,byteBuf.readableBytes()-signIndexLength);
        }


        ProtoMsg.LoginRequest loginRequest = ProtoMsg.LoginRequest.parseFrom(msgBytes);
        log.info(loginRequest.getToken());

        msgStr = new String(msgBytes);
        log.info("标识符,{}",sign);
        log.info("服务器二进制接收到的内容: {} ",msgStr);


        /**
         *  组成相应的json字符串
         *  然后 使用 invocationBussinessJson 统一处理业务逻辑
         *  通过进入的是二进制、文本、还是protobuf 相应的请求类型做返回
         *  可以通过请求的到的Handler设置一个handler和发送消息的一个绑定关系
         */
        messageSendUtil.responseTheRequestByBinary(ctx.channel(), ResultUtil.success("服务端收到消息了,你的消息内容是:"+msgStr) );

    }
}
